export default (e) => {
	return new Promise((resolve, reject) => {
		const {
			encryptedData,
			rawData,
			iv,
			signature
		} = e.detail
		uni.getSetting({
			success: (res) => {
				// 判断用户是否授权
				if (res.authSetting['scope.userInfo']) {
					// 此处是为了得到调用wx.login后得到的code，但是uniapp需要如下方法来判断服务商是微信后才能调用
					uni.getProvider({
						service: 'oauth',
						success: (res) => {
							if (res.provider.indexOf('weixin') > -1) {
								uni.login({
									provider: 'weixin',
									success: (res) => {
										// 需要有如下参数才能获取token
										uni.request({
											url: 'https://api-hmugo-web.itheima.net/api/public/v1/users/wxlogin',
											method: 'post',
											data: {
												encryptedData,
												rawData,
												iv,
												signature,
												code: res.code
											},
											success: (res) => {
												const token = res.data.message.token
												resolve(token)
											}
										})
									}
								});
							}
						}
					});
				}
			}
		})
	})
}
